<template>
	<view class="jnpf-wrap jnpf-wrap-workflow">
		<u-form :model="dataForm" ref="dataForm" :errorType="['toast']" label-position="left" label-width="150"
			label-align="left">
			<u-form-item label="退料单别" v-if="judgeShow('tc001')">
				<u-input v-model="dataForm.tc001" placeholder="采购单别" :disabled="judgeWrite('tc001')"></u-input>
			</u-form-item>
			<u-form-item label="退料单号" v-if="judgeShow('tc002')">
				<u-input v-model="dataForm.tc002" placeholder="退料单号" :disabled="judgeWrite('tc002')"></u-input>
			</u-form-item>
			<u-form-item label="单据日期" v-if="judgeShow('tc014')">
				<u-input v-model="dataForm.tc014" placeholder="单据日期" :disabled="judgeWrite('tc014')"></u-input>
			</u-form-item>
			<u-form-item label="工厂" v-if="judgeShow('tc004')">
				<u-input v-model="dataForm.tc004" placeholder="工厂" :disabled="judgeWrite('tc004')"></u-input>
			</u-form-item>
			<u-form-item label="退料日期" v-if="judgeShow('tc003')">
				<u-input v-model="dataForm.tc003" placeholder="退料日期" :disabled="judgeWrite('tc003')"></u-input>
			</u-form-item>
			<u-form-item label="退料人员" v-if="judgeShow('tc028')">
				<jnpf-user-select v-model="dataForm.tc028" :disabled="judgeWrite('tc028')" />
			</u-form-item>
			<u-form-item label="工作中心" v-if="judgeShow('tc005')">
				<u-input v-model="dataForm.tc005" placeholder="工作中心" :disabled="judgeWrite('tc005')"></u-input>
			</u-form-item>
			<u-form-item label="委外供应商" v-if="judgeShow('tc006')">
				<u-input v-model="dataForm.tc006" placeholder="委外供应商" :disabled="judgeWrite('tc006')"></u-input>
			</u-form-item>
			<u-form-item label="部门编号" v-if="judgeShow('tc021')">
				<jnpf-dep-select v-model="dataForm.tc021" :disabled="judgeWrite('tc021')"></jnpf-dep-select>
			</u-form-item>
			<u-form-item label="审核者" v-if="judgeShow('tc015')">
				<u-input v-model="dataForm.tc015" placeholder="审核者" :disabled="judgeWrite('tc015')"></u-input>
			</u-form-item>
			<u-form-item label="生成分录" v-if="judgeShow('tc011')">
				<u-input v-model="dataForm.tc011" placeholder="生成分录" :disabled="judgeWrite('tc011')"></u-input>
			</u-form-item>

			<u-form-item label="生成按序" v-if="judgeShow('tc012')">
				{{dataForm.tc012=='1'?'按工单单号':'按材料单号'}}
			</u-form-item>
			<u-form-item label="来源单别" v-if="judgeShow('tc019')">
				<u-input v-model="dataForm.tc019" placeholder="来源单别" :disabled="judgeWrite('tc019')"></u-input>
			</u-form-item>
			<u-form-item label="来源单号" v-if="judgeShow('tc020')">
				<u-input v-model="dataForm.tc020" placeholder="来源单号" :disabled="judgeWrite('tc020')"></u-input>
			</u-form-item>
			<u-form-item label="签核状态码" v-if="judgeShow('tc016')">
				<u-input v-model="dataForm.tc016" placeholder="签核状态码" :disabled="judgeWrite('tc016')"></u-input>
			</u-form-item>
			<u-form-item label="项目编号" v-if="judgeShow('tc031')">
				<u-input v-model="dataForm.tc031" placeholder="项目编号" :disabled="judgeWrite('tc031')"></u-input>
			</u-form-item>
			<u-form-item label="备注" v-if="judgeShow('tc007')">
				<u-input v-model="dataForm.tc007" placeholder="备注" :disabled="judgeWrite('tc007')"></u-input>
			</u-form-item>
			<u-form-item label="附件" v-if="judgeShow('attachmentUpload')">
				<jnpf-file v-model="dataForm.attachmentUpload" :disabled="
			  judgeWrite('attachmentUpload')" />
			</u-form-item>

			<view class="jnpf-table" style="margin-top: 24rpx;">
				<view class="jnpf-table-item" style="margin-bottom: 24rpx;" v-for="(item,i) in dataForm.mocteList"
					:key="i">
					<u-form-item label="序号">
						{{i+1}}
					</u-form-item>
					<u-form-item label="工单单别">
						<u-input v-model="item.te011"></u-input>
					</u-form-item>
					<u-form-item label="工单单号">
						<u-input v-model="item.te012"></u-input>
					</u-form-item>
					<u-form-item label="材料品号">
						<u-input v-model="item.te004"></u-input>
					</u-form-item>
					<u-form-item label="品名">
						<u-input v-model="item.te017"></u-input>
					</u-form-item>
					<u-form-item label="规格">
						<u-input v-model="item.te018"></u-input>
					</u-form-item>
					<u-form-item label="需领料量">
						<u-input v-model="item.te031"></u-input>
					</u-form-item>
					<u-form-item label="未领料量">
						<u-input v-model="item.te032"></u-input>
					</u-form-item>
					<u-form-item label="领退料数量">
						<u-input v-model="item.te027"></u-input>
					</u-form-item>
					<u-form-item label="单位">
						<u-input v-model="item.te006"></u-input>
					</u-form-item>
					<u-form-item label="领料包装数量">
						<u-input v-model="item.te021"></u-input>
					</u-form-item>
					<u-form-item label="仓库">
						<u-input v-model="item.te008"></u-input>
					</u-form-item>
					<u-form-item label="工艺">
						<u-input v-model="item.te009"></u-input>
					</u-form-item>
					<u-form-item label="批号">
						<u-input v-model="item.te010"></u-input>
					</u-form-item>
					<u-form-item label="批号说明">
						<u-input v-model="item.te023"></u-input>
					</u-form-item>
					<u-form-item label="产品品号">
						<u-input v-model="item.cta_ta006"></u-input>
					</u-form-item>
					<u-form-item label="产品品名">
						<u-input v-model="item.cta_ta034"></u-input>
					</u-form-item>
					<u-form-item label="产品规格">
						<u-input v-model="item.cta_ta035"></u-input>
					</u-form-item>
					<u-form-item label="材料类型">
						<span v-if="item.te016==1">直接材料</span>
						<span v-if="item.te016==2">间接材料</span>
						<span v-if="item.te016==3">供应商供料</span>
						<span v-if="item.te016==4">不发料</span>
						<span v-if="item.te016==5">客户供料</span>
					</u-form-item>
					<u-form-item label="领料说明">
						<u-input v-model="item.te013"></u-input>
					</u-form-item>
					<u-form-item label="备注">
						<u-input v-model="item.te014"></u-input>
					</u-form-item>
					<u-form-item label="包材品号">
						<u-input v-model="item.udf02"></u-input>
					</u-form-item>
					<u-form-item label="包材品名">
						<u-input v-model="item.udf03"></u-input>
					</u-form-item>
					<u-form-item label="包材数量">
						<u-input v-model="item.udf54"></u-input>
					</u-form-item>


				</view>
			</view>
			<u-form-item label="总需领料量" v-if="judgeShow('sumNeed')">
				<u-input v-model="dataForm.sumNeed" placeholder="总需领料量" :disabled="judgeWrite('sumNeed')"></u-input>
			</u-form-item>
			<u-form-item label="总未领料量" v-if="judgeShow('sumNot')">
				<u-input v-model="dataForm.sumNot" placeholder="总未领料量" :disabled="judgeWrite('sumNot')"></u-input>
			</u-form-item>
			<u-form-item label="总领料数量" v-if="judgeShow('tc029')">
				<u-input v-model="dataForm.tc029" placeholder="总领料数量" :disabled="judgeWrite('tc029')"></u-input>
			</u-form-item>
		</u-form>
	</view>
</template>

<script>
	import {
		getMoctc
	} from '@/api/workFlow/flowEngine'
	import {
		getDataInterfaceDataInfo,
		getDictionaryDataSelector,
		getDataInterfaceRes,
		getUserAll,
		Interface,
	} from '@/api/common'

	import comMixin from '../mixin'
	import {
		Info,
		Create,
		Update
	} from '@/api/apply/order'
	export default {
		name: 'crmOrder',
		mixins: [comMixin],
		watch: {
			//总需领料量，总未领料量，总领料量
			"dataForm.mocteList": {
				handler(newVal, oldVal) {
					let sumNeed = 0;
					let sumNot = 0;
					let sumTc029 = 0;
					for (let i = 0; i < newVal.length; i++) {
						const e = newVal[i];
						sumNeed += parseFloat(e.ctb_Tb004); //需领用量
						sumNot += parseFloat(e.ctb_Tb004005); //未领用量
						sumTc029 += parseFloat(e.te005); //总领料量
					}
					this.dataForm.sumNeed = sumNeed;
					this.dataForm.sumNot = sumNot;
					this.dataForm.tc029 = sumTc029;
				},
				deep: true,
			},
		},
		data() {
			return {
				dataForm: {},
				id: '',
				dataForm: {},
				udf13Name: [],
				tc011Name: [],
				sourceList: [],
				ti006Name: '',
				ti007Name: '',
			}
		},

		methods: {
			init(data, id, flowFormContentJson) {
				this.setting = data
				console.log('this.setting', this.setting);
				this.id = id
				this.getInfo(data)
			},
			getInfo(data) {
				let list = data.id.split("-");
				let obj = {
					tc001: list[1],
					tc002: list[2]
				}
				getMoctc(obj).then(res => {
					console.log('res', res);
					if (res.code == 200) {
						this.dataForm = res.data

						this.getDepartment(this.dataForm.ta004)
						this.getPersonnel(this.dataForm.ti007)
						for (let i = 0; i < this.dataForm.mocteList.length; i++) {
							this.getWarehouse(this.dataForm.mocteList[i].te008, i)
						}
						this.getSource()
					}
				})
			},
			judgeShow(id) {
				if (!this.setting.formOperates || !this.setting.formOperates.length)
					return true;
				let arr = this.setting.formOperates.filter((o) => o.id === id) || [];
				if (!arr.length) return true;
				let item = arr[0];
				return item.read;
			},
			judgeWrite(id) {
				if (!this.setting.formOperates || !this.setting.formOperates.length)
					return false;
				let arr = this.setting.formOperates.filter((o) => o.id === id) || [];
				if (!arr.length) return true;
				let item = arr[0];
				return !item.write;
			},
			getUser() {
				getUserAll().then((res) => {
					this.udf13Name = res.data.filter((item) => this.dataForm.udf13.includes(item.id)).map((item) =>
						item.realName);
					this.tc011Name = res.data.filter((item) => this.dataForm.tc028.includes(item.account)).map((
							item) =>
						item.realName);
				});
			},
			// 获取部门
			getDepartment(id) {
				let interfaceId = '362111557984852549'
				let data = {
					id,
					interfaceId,
					propsValue: 'ME001',
					relationField: 'JoinStr',
				}
				getDataInterfaceDataInfo(interfaceId, data).then(res => {
					this.ti006Name = res.data.JoinStr;
				})
			},
			// 获取人员
			getPersonnel(id) {
				let interfaceId = '438122284524265733'
				let data = {
					id,
					interfaceId,
					propsValue: 'MV001',
					relationField: 'JoinStr',
				}
				getDataInterfaceDataInfo(interfaceId, data).then(res => {
					this.ti007Name = res.data.JoinStr;
				})
			},
			// 获取仓库
			getWarehouse(id, index) {
				let interfaceId = '362114335423601221'
				let data = {
					id,
					interfaceId,
					propsValue: 'MC001',
					relationField: 'MC001',
				}
				getDataInterfaceDataInfo(interfaceId, data).then(res => {
					this.dataForm.mocteList[index].te008 = res.data.MC002

				})
			},
			getSource() {
				getDictionaryDataSelector("474002610777559109").then(res => {
					this.sourceList = res.data.list.filter((item) => this.dataForm.ta009.includes(item.enCode))
						.map((
								item) =>
							item.fullName);
				});
			},
			selfGetInfo() {
				Info(this.setting.id).then(res => {
					this.dataForm = res.data
					if (this.dataForm.fileJson) {
						this.fileList = JSON.parse(this.dataForm.fileJson)
					}
				})
			},
			selfSubmit() {
				this.dataForm.status = this.eventType === 'submit' ? 0 : 1
				if (this.eventType === 'save') return this.selfHandleRequest()
				uni.showModal({
					title: '提示',
					content: '您确定要提交当前流程吗？',
					success: res => {
						if (res.confirm) {
							this.selfHandleRequest()
						}
					}
				})
			},
			selfHandleRequest() {
				if (!this.dataForm.id) delete(this.dataForm.id)
				const formMethod = this.dataForm.id ? Update : Create
				formMethod(this.dataForm).then(res => {
					uni.showToast({
						title: res.msg,
						complete: () => {
							setTimeout(() => {
								uni.$emit('refresh')
								uni.navigateBack()
							}, 1500)
						}
					})
				})
			},

		}
	}
</script>